const multer = require('multer');
const path = require('path')

var uploadfile = (req, res, next) => {

  let fullpath = path.resolve(__dirname, '../public/upload')

  let filename = '';

  var storage = multer.diskStorage({
    destination: function (req, file, cb) {
      cb(null, fullpath)
    },
    filename: function (req, file, cb) {
      // console.log('file:', req)
      //获取文件的后缀
      let extname = path.extname(file.originalname)
      filename = file.fieldname + '-' + Date.now() + extname;
      cb(null, filename)
    }
  })

  var upload = multer({ storage }).single('img')

  upload(req, res, function (err) {
    if (!req.file) {
      delete req.body['companyLogo']
      next();
      return;
    }

    if (err) {
      res.send({
        code: -1,
        message: '图片上传失败'
      })
    } else {
      req.body.img = filename;
      next();
    }

  })


  console.log('upload file ..')
  // next();
}


module.exports = uploadfile;